<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property integer $id
 * @property integer $is_show
 * @property string $icon
 * @property string $title
 * @property string $content
 * @property integer $classify
 * @property integer $sort
 * @property integer $created_at
 * @property integer $updated_at
 * @property File $file
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'icon'], 'required'],
            [['content'], 'string'],
            [['is_show', 'classify', 'sort', 'created_at', 'updated_at'], 'integer'],
            [['icon', 'title'], 'string', 'max' => 255],
        ];
    }
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '序号',
            'is_show' => '显示状态',
            'icon' => '图标',
            'title' => '标题',
            'content' => '内容',
            'classify' => '分类',
            'sort' => '排序',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFile()
    {
        return $this->hasOne(File::className(), ['file_id' => 'icon']);
    }
    
    /**
     * 得到文章的分类信息
     */
    public function getClassify()
    {
        return [
            1 => '会员须知',
            2 => '会员增值',
            3 => '会员好处',
        ];
    }
}
